<?php

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;

class WxController extends BaseController
{
    protected $only;
    protected $except;

    public function __construct()
    {
        $option = [];
        if(!is_null($this->only)){
            $option['only'] = $this->only;
        }
        if(!is_null($this->except)){
            $option['except'] = $this->except;
        }
        $this->middleware('auth:wx',$option);
    }

    public function codeReturn(array $codeResponse,$data = null,$info = '')
    {
        list($errno,$errmsg) = $codeResponse;
        $ret = ['errno'=>$errno,'errmsg'=>$info?:$errmsg];
        if(!is_null(($data))){
            $ret['data']= $data;
        }
        return response()->json($ret);
    }

    protected function success($data = null){
        return $this->codeReturn(CodeResponse::SUCCESS,$data);
    }
    protected function fail(array $codeResponse = CodeResponse::FAIL,$info = ''){
        return $this->codeReturn($codeResponse,null,$info);
    }

    protected function failOrSuccess($isSuccess,array $codeResponse = CodeResponse::FAIL,$data = null,$info = ''){
        if($isSuccess){
            return $this->success($data);
        }
        return $this->fail($codeResponse,$info);
    }

    /**
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function user(){
        return Auth::guard('wx')->user();
    }
}
